#coding=UTF-8 
# from selenium.webdriver.common.action_chains import ActionChains


from chinacQA.TestLink_TestCase import assertBool
from chinacQA.TestLink_TestCase import getConfigure
from chinacQA.TestLink_TestCase import waitMoment

from chinacQA.TestCase import TestCase
from template.NeoCUUI import *
from TC_006_NeoCUServer import logonOut
from TC_006_NeoCUServer import STEP_deleteRubbish

def Login(user, pw):
    idusername.text=user
    idpassword.text=pw
    loginBtn.click()

def STEP_UserLogin():
    Login(getConfigure("User"),getConfigure("UserNewPassword"))


def STEP_CreateCache():
    CacheMenu.click()
    waitMoment(3)
    cachedatabases__action_launch.click()
    Cacheidname.text = getConfigure("CacheName_01")
    CacheSizeChoose.click()
    menus = CacheSizeChoose.element.find_elements("xpath",".//li")
    for buttonm in menus:
        if (getConfigure("CacheSize_01") in buttonm.text.strip()):
            buttonm.click()
            break
    
    CacheNetworkChoose.click()
    menusNetwork = CacheNetworkChoose.element.find_elements("xpath",".//li")
    for button in menusNetwork:
        if (getConfigure("net") in button.text.strip()):
            button.click()
            break
        
    CacheVersionChoose.click()
    menus = CacheVersionChoose.element.find_elements("xpath",".//li")
    for buttonm in menus:
        if (getConfigure("CacheVesion_01") in buttonm.text.strip()):
            buttonm.click()
            break
        
    CacheHaModeChoose.click()
    menus = CacheHaModeChoose.element.find_elements("xpath",".//li")
    for buttonm in menus:
        if (getConfigure("CacheHAMode_01") in buttonm.text.strip()):
            buttonm.click()
            break
        
    CacheCreate.click()
    
    heads = cachedatabasesTable.getHeads()
    columnName = heads.index('名称')
#     columnAction = heads.index('操作')
    columnStatus = heads.index('状态')
    for i in range(24):
        waitMoment(15)
        for i in range(len(cachedatabasesTable.getRows())):
            if (cachedatabasesTable.getCell(i,columnName) !=None and cachedatabasesTable.getCell(i,columnName).text.strip()== (getConfigure("CacheName_Prefix") + getConfigure("CacheName_01"))):
                if(cachedatabasesTable.getCell(i,columnStatus).text.strip() == "运行"):
                    assertBool(True,"3507")
                    return 
    assertBool(False,"3507")
    
def STEP_EditCache():
    CacheMenu.click()
    waitMoment(3)
    if("没有要显示的条目" in cachedatabasesTable.getCell(0,0).text):
        assertBool(False,"3509")
        return
    heads = cachedatabasesTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    columnStatus = heads.index('状态')
    
    for i in range(len(cachedatabasesTable.getRows())):
        if(cachedatabasesTable.getCell(i,columnName).text.strip()==(getConfigure("CacheName_Prefix") + getConfigure("CacheName_01"))):
            actioncell=cachedatabasesTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="编辑实例"):
                    button.click()
                    break
    
    CacheEditidname.text = getConfigure("CacheNewName")
    CacheEditCommit.click()
    waitMoment(3)
    CacheMenu.click()
    waitMoment(3)
    for i in range(len(cachedatabasesTable.getRows())):
        if (cachedatabasesTable.getCell(i,columnName).text.strip()== getConfigure("CacheNewName")):
            assertBool(True,"3509")
            return 
    assertBool(False,"3509")
    
    
def STEP_RestartCache():
    CacheMenu.click()
    waitMoment(3)
    if("没有要显示的条目" in cachedatabasesTable.getCell(0,0).text):
        assertBool(False,"3511")
        return
    heads = cachedatabasesTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    columnStatus = heads.index('状态')
    
    for i in range(len(cachedatabasesTable.getRows())):
        if(cachedatabasesTable.getCell(i,columnName).text.strip()==getConfigure("CacheNewName")):
            actioncell=cachedatabasesTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="重启实例"):
                    button.click()
                    break
    CacheRestart.click()
  
    for j in range(200):
        waitMoment(6)
        for i in range(len(cachedatabasesTable.getRows())):
            if (cachedatabasesTable.getCell(i,columnName)!=None and cachedatabasesTable.getCell(i,columnName).text.strip()== getConfigure("CacheNewName")):
                if(cachedatabasesTable.getCell(i,columnStatus)!=None and cachedatabasesTable.getCell(i,columnStatus).text.strip() == "运行"):
                    assertBool(True,"3511")
                    return 
    assertBool(False,"3511")
    
def STEP_DeleteCache():
    CacheMenu.click()
    waitMoment(6)
    if("没有要显示的条目" in cachedatabasesTable.getCell(0,0).text):
        assertBool(False,"3513")
        return
    heads = cachedatabasesTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    columnStatus = heads.index('状态')
    
    for i in range(len(cachedatabasesTable.getRows())):
        if(cachedatabasesTable.getCell(i,columnName).text.strip()==getConfigure("CacheNewName")):
            actioncell=cachedatabasesTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="删除缓存"):
                    button.click()
                    break
    CacheDelete.click()
    waitMoment(3)
    
    for i in range(10):
        status = True
        waitMoment(30)
        CacheMenu.click()
        waitMoment(30)
        if("没有要显示的条目" in cachedatabasesTable.getCell(0,0).text):
            assertBool(True,"3513")
            return
        for i in range(len(cachedatabasesTable.getRows())):
            if (cachedatabasesTable.getCell(i,columnName)!=None and cachedatabasesTable.getCell(i,columnName).text.strip()== getConfigure("CacheNewName")):
                status = False
        if(status):
            break
               
    assertBool(status,"3513")
    
    

    
def STEP_Clean():
    pass


TC_009_NeoCUCache = TestCase("Neocu Cache")
TC_009_NeoCUCache.AddStep(STEP_UserLogin)
TC_009_NeoCUCache.AddStep(STEP_CreateCache)
TC_009_NeoCUCache.AddStep(STEP_EditCache)
TC_009_NeoCUCache.AddStep(STEP_RestartCache)
TC_009_NeoCUCache.AddStep(STEP_DeleteCache)

if __name__ == "__main__":
    TC_009_NeoCUCache.run()

    


    
